<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/07/18
 * Time: 10:06
 */

class ORCController extends BaseController {

    /**
     * @Author:          sqq
     * @DateTime:        2018-07-18 上午10:12
     * @Description:     C端图片上传
     * @param string $token             用户token
     * @param file $file                图片
     * @link
     */
    public function UploadFile() {
        header('Access-Control-Allow-Origin:*');
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'file'=> 'required',   //最大上传2M
                //'file_type'=> 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
//            if($validator->fails()) {
//                $response = Lang::get("ahp_error.args_error_1000");
//                $response["result"] = $validator->messages()->all();
//                break;
//            }
            $res = $this->verificationUserToken(Input::get("token"),$response);
            if(is_null($res)){
                break;
            }
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }

            $admin_id       = $res->id;             //管理员id
            $upload_file    = Input::file('file');          //上传文件
            $file_type      = Input::get('file_type');      //文件类型 (1:图片;2:非图片;)
            if($upload_file->isValid()){
                //文件上传成功
                $name = $upload_file->getClientOriginalName();  //文件原名称

                //$extension = $upload_file->getClientOriginalExtension(); //扩展名
                $storage_name = date('Ymdhis', time()).'_'.$name;               //存储名称
                $upload_file->move(Config::get('config.ORC_upload_files'), $storage_name);
                $file['storage_name'] = Config::get('config.ORC_upload_files_url').$storage_name;
                $file['name'] = $storage_name;
            } else {
                $response = Lang::get("error.wechat_file_error_30010");
                $response["result"] = $validator->messages()->all();
                break;
            }

            $response = [
                'code' => 0,
                'msg' => 'ok',
                'result' => $file,
            ];
        }while(false);

        Log::info("info", array("system" => "api3.0" ,"event" => "wechat/api3.0/UploadFile", "input" =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    public function cardInfo() {
        $filepath = Input::file('file');
        $ocr = $this->cardOCR ( $filepath );
        if ($ocr) {
            if ($ocr ['message'] ['status'] == 0) {
                $cardinfo = $ocr ['cardsinfo'] [0];
                foreach ( $cardinfo ['items'] as $k => $v ) {
                    if ($v ['desc'] == "姓名") {
                        $card ['name'] = $v ['content'];
                    }
                    if ($v ['desc'] == "职务/部门") {
                        $card ['position'] = $v ['content'];
                    }
                    if ($v ['desc'] == "公司") {
                        $card ['company'] = $v ['content'];
                    }
                    if ($v ['desc'] == "手机") {
                        $card ['mobile'] [] = $v ['content'];
                    }
                    if ($v ['desc'] == "电话") {
                        $card ['tel'] = $v ['content'];
                    }
                    if ($v ['desc'] == "传真") {
                        $card ['fax'] = $v ['content'];
                    }
                    if ($v ['desc'] == "电子邮箱") {
                        $card ['email'] = $v ['content'];
                    }
                    if ($v ['desc'] == "网址") {
                        $card ['www'] = $v ['content'];
                    }
                    if ($v ['desc'] == "邮编") {
                        $card ['postcode'] = $v ['content'];
                    }
                    if ($v ['desc'] == "vin") {
                        $card ['postcode'] = $v ['content'];
                    }
                }
            }
        } else {
            $card = $ocr;
        }
        return $card;
    }


    // 远程提交OCR获取数据
    public function cardOCR($filepath) {
        $filepath = realpath ($filepath);
        $file = array (
            "key" => "7htxNWqdpY1F4FD3xgaGpG",
            "secret" => "d78b3e653a8148838a9ccf53f73a5c2f",
            "typeId" => "2007",
            "format" => "json",
            "file" => new \CURLFile ( $filepath, 'image/jpeg' )
        );
        $curl = curl_init ();
        curl_setopt ( $curl, CURLOPT_URL, "http://netocr.com/api/recog.do" );
        curl_setopt ( $curl, CURLOPT_POST, true );
        curl_setopt ( $curl, CURLOPT_POSTFIELDS, $file );
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
        $result = curl_exec ( $curl );
        curl_close ( $curl );
        return json_decode ( $result, true );
    }

}